package com.school_admin.controller;

import com.school_admin.config.Result;
import com.school_admin.entity.Feedback;
import com.school_admin.mapper.FeedbackMapper;
import com.school_admin.service.impl.FeedbackServiceImpl;
import com.school_admin.util.CreateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-03-18
 */
@RestController
@RequestMapping("/feedback")
public class FeedbackController {
    private final FeedbackServiceImpl feedbackService;
    private final CreateTime createTime;
    private final FeedbackMapper feedbackMapper;

    @Autowired
    public FeedbackController(
            FeedbackServiceImpl feedbackService,
            CreateTime createTime, FeedbackMapper feedbackMapper) {
        this.feedbackService = feedbackService;
        this.createTime = createTime;
        this.feedbackMapper = feedbackMapper;
    }

    // 创建留言反馈
    @PostMapping("create")
    public Result<?> createFeedBack(Feedback feedback) {
        // 插入操作
        feedback.setTime(createTime.getTime());
        boolean is = feedbackService.save(feedback);
        if (is) {
            return Result.success("反馈成功");
        }
        return Result.fail("反馈失败");
    }

    // 删除留言反馈
    @DeleteMapping("remove/{id}")
    public Result<?> deleteFeedBack(@PathVariable int id) {
        boolean is = feedbackService.removeById(id);
        if (is) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    // 查询单个留言反馈
    @GetMapping("find/{id}")
    public Result<?> getFeedBack(@PathVariable int id) {
        List<Map> list = feedbackMapper.find(id); // 查询操作
        if (list.size() > 0) {
            return Result.success(list.get(0));
        }
        return Result.fail("查询失败");
    }

    // 获取全部留言反馈
    @GetMapping("list")
    public Result<?> getFeedBackList(Integer page, Integer pageSize) {
        //  查询操作
        int start = (page - 1) * pageSize;
        List<Map<String, Object>> list = feedbackMapper.getList(start, pageSize);
        if (list.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("list", list);
            map.put("total", feedbackMapper.getCount());
            return Result.success(map);
        }
        return Result.fail("留言数据为空");
    }

    // 用户获取的自己的反馈列表
    @GetMapping("userFeedBackList")
    public Result<?> getUserFeedBackList(Integer page, Integer pageSize, String uid) {
        //  查询操作
        int start = (page - 1) * pageSize;
        List<Map<String, Object>> list = feedbackMapper.getUserFeedBackList(start, pageSize, uid);
        if (list.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("list", list);
            map.put("total", feedbackMapper.getUserFeedBackCount(uid));
            return Result.success(map);
        }
        return Result.fail("用户反馈数据为空");
    }

    // 管理员回复留言
    @PostMapping("reply")
    public Result<?> replyFeedBack(Feedback feedback) {
        System.out.println("feedback = " + feedback);
        int i = feedbackMapper.updateById(feedback);
        if (i > 0) {
            return Result.success("回复成功");
        }
        return Result.fail("回复失败");
    }

    // 用户删除
    @DeleteMapping("deleteUser/{id}")
    public Result<?> deleteFeedBack(@PathVariable Integer id) {
        boolean i = feedbackMapper.deleteUser(id);
        if (i) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }
}
